package com.atguigu.controller.api;

import com.atguigu.common.exception.YyghException;
import com.atguigu.common.resultdemo.Result;
import com.atguigu.common.utils.HttpRequestHelper;
import com.atguigu.common.utils.MD5;
import com.atguigu.service.DepartmentService;
import com.atguigu.service.HospitalService;
import com.atguigu.service.HospitalSetService;
import com.atguigu.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;

    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        //将集合中的数组转变为 Object集合
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //进行参数校验
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(20001, "医院编号为空");
        }
        //签名校验
        String sign = (String) paramMap.get("sign");
        //根据传过来的编码，查询数据库，查看签名（数据库中是未加密的）
        String signKey = hospitalSetService.getSignKey(hoscode);
        String encrypt = MD5.encrypt(signKey);
        //判断签名是否一致
        //1如果不一致
        if (!sign.equals(encrypt)) {
            throw new YyghException(20001, "签名失败");
        }
        //logoData中的 空格 替换成 +
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);
        //调用hosptialService 将集合进行添加
        hospitalService.save(paramMap);
        return Result.ok();
    }

    @PostMapping("hospital/show")
    public Result hosptial(HttpServletRequest request) {
        //将数据转换为json
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(20001, "医院编号为空");
        }
        Hospital hospital = hospitalService.getHoscode(hoscode);
        return Result.ok(hospital);
    }

    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());
        departmentService.save(stringObjectMap);
        return Result.ok();
    }

    //科室显示页面
    @PostMapping("department/list")
    public Result department(HttpServletRequest request) {
        //将数据转化为 String object类型的
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        //获取医院编号
        String hoscode = (String) map.get("hoscode");
        //获取科室编号
        String limit = (String) map.get("limit");
        //获取数据页数
        String page = (String) map.get("page");
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> departments = departmentService.selectPage(Integer.parseInt(limit), Integer.parseInt(page), departmentQueryVo);
        return Result.ok(departments);
    }

    //删除科室
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        //先找到 医院编号
        String hoscode = (String) map.get("hoscode");
        //再找到 科室编号
        String depcode = (String) map.get("depcode");
        departmentService.deleteByHoscodeAndDepcode(hoscode, depcode);
        return Result.ok();
    }

    //排班接口
    @Autowired
    private ScheduleService scheduleService;

    @PostMapping("saveSchedule")
    public Result saveScheduleService(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        //进行数据类型转换
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        scheduleService.save(map);
        return Result.ok();
    }

    //排班chaxun
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) map.get("hoscode");
        String depcode = (String) map.get("depcode");
        int page = Integer.parseInt((String) map.get("page"));
        int limit = Integer.parseInt((String) map.get("limit"));

        //封装在 ScheduleVo中
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        //调用sevice 方法
        Page<Schedule> selectPage = scheduleService.selectPage(page, limit, scheduleQueryVo);
        return Result.ok(selectPage);
    }

    //删除排班
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        //1、获取参数
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        //2、调用service方法
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }


}
